from pyglet.gl import glEnable
from pyglet.gl import glDisable
from pyglet.gl import glBegin
from pyglet.gl import glEnd
from pyglet.gl import GL_TRIANGLES
from pyglet.gl import GL_QUADS
from pyglet.gl import GL_POLYGON
from pyglet.gl import GL_LINE_STRIP
from pyglet.gl import GL_LINES
from pyglet.gl import glScissor
from pyglet.gl import GL_SCISSOR_TEST
from pyglet.gl import gluNewQuadric
from pyglet.gl import gluDeleteQuadric
from pyglet.gl import gluPartialDisk
from pyglet.gl import glVertex2f
from pyglet.gl import glColor3f
from pyglet.gl import glTranslatef
from pyglet.gl import GL_BLEND
from pyglet.gl import glBlendFunc
from pyglet.gl import GL_SRC_ALPHA
from pyglet.gl import GL_ONE_MINUS_SRC_ALPHA
from pyglet.gl import glColor4f
from pyglet.gl import glLoadIdentity
from rect import Rect


def draw_func(rect, points, modifiers, glfunc):
    glBegin(glfunc)
    for nmb, p in enumerate(points):

        glVertex2f(getattr(rect, p[0])+modifiers[nmb][0],
                   getattr(rect, p[1])+modifiers[nmb][1])

    glEnd()


class BasicView(object):
    def __init__(self, rect):
        self._rect = rect
        self._working_rect = Rect(left=rect.left+1,
                                  right=rect.right,
                                  bottom=rect.bottom+1,
                                  top=rect.top,
                                  stick='topright')


    def draw(self, rect):
        
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        if rect:
            glEnable(GL_SCISSOR_TEST)
            glScissor(rect.left, rect.bottom, rect.width, rect.height)

        self._draw_routine()
        
        if rect:
            glDisable(GL_SCISSOR_TEST)

        glDisable(GL_BLEND)


    def _draw_routine(self):

        self._working_rect.width = self._rect.width - 1
        self._working_rect.height = self._rect.height - 1
        self._working_rect.right = self._rect.right
        self._working_rect.top = self._rect.top




class BasicClearView(BasicView):

    def __init__(self, rect):
        BasicView.__init__(self, rect)
        self._points = [('left', 'bottom'),
                        ('left', 'top'),
                        ('right', 'top'),
                        ('right', 'bottom'),
                        ('left', 'bottom')]
        
        self._modifiers = [(0, 0),
                           (0, 0),
                           (0, 0),
                           (0, 0),
                           (-1, 0)]

        self._topleft = [('left', 'bottom'),
                         ('left', 'top'),
                         ('right', 'top')]
        
        self._topleft_modifiers1 = [(1, 1),
                                    (1, -1),
                                    (-1, -1)]
        
        self._topleft_modifiers2 = [(2, 2),
                                    (2, -2),
                                    (-2, -2)]

        self._bottomright = [('right', 'top'),
                             ('right', 'bottom'),
                             ('left', 'bottom')]

        self._bottomright_modifiers1 = [(-1, -1),
                                        (-1, 1),
                                        (1, 1)]

        self._bottomright_modifiers2 = [(-2, -2),
                                        (-2, 2),
                                        (2, 2)]


class BasicBeveledView(BasicView):

    def __init__(self, rect):
        BasicView.__init__(self, rect)
        self._corner_size = 3

        self._points = [('left', 'bottom'),
                        ('left', 'bottom'),
                        ('left', 'top'),
                        ('left', 'top'),
                        ('right', 'top'),
                        ('right', 'top'),
                        ('right', 'bottom'),
                        ('right', 'bottom'),
                        ('left', 'bottom')]
        
        self._modifiers = [(self._corner_size-1, 0),
                           (0, self._corner_size-1),
                           (0, self._corner_size*-1),
                           (self._corner_size, 0),
                           (self._corner_size *-1 -1, 0),
                           (0, self._corner_size *-1 -1),
                           (0, self._corner_size),
                           (self._corner_size *-1, 0),
                           (self._corner_size -1, 0)]






# foreground views #########################################################
class Border(BasicClearView):
    def _draw_routine(self):
        BasicClearLay._draw_routine(self)

        glLoadIdentity()
        glColor4f(0, 0, 0, 0.25)
        draw_func(self._working_rect, self._points,
                  self._modifiers, GL_LINE_STRIP)


class Outlay(Border):
    def _draw_routine(self):
        Border._draw_routine(self)

        # lights
        glColor4f(1, 1, 1, 0.5)
        draw_func(self._working_rect, self._topleft,
                  self._topleft_modifiers1, GL_LINE_STRIP)

        # shadows
        glColor4f(0, 0, 0, 0.25)
        draw_func(self._working_rect, self._bottomright,
                  self._bottomright_modifiers1, GL_LINE_STRIP)

        #lights
        glColor4f(1, 1, 1, 0.25)
        draw_func(self._working_rect, self._topleft,
                  self._topleft_modifiers2, GL_LINE_STRIP)

        #shadows
        glColor4f(0, 0, 0, 0.125)
        draw_func(self._working_rect, self._bottomright,
                  self._bottomright_modifiers2, GL_LINE_STRIP)


class Inlay(Border):
    def _draw_routine(self):    
        Border._draw_routine(self)

        # shadows
        glColor4f(0, 0, 0, 0.25)
        draw_func(self._working_rect, self._topleft,
                  self._topleft_modifiers1, GL_LINE_STRIP)

        # lights
        glColor4f(1, 1, 1, 0.5)
        draw_func(self._working_rect, self._bottomright,
                  self._bottomright_modifiers1, GL_LINE_STRIP)

        # shadows
        glColor4f(0, 0, 0, 0.125)
        draw_func(self._working_rect, self._topleft,
                  self._topleft_modifiers2, GL_LINE_STRIP)

        # lights
        glColor4f(1, 1, 1, 0.25)
        draw_func(self._working_rect, self._bottomright,
                  self._bottomright_modifiers2, GL_LINE_STRIP)




class BeveledBorder(BasicBeveledView):
    def _draw_routine(self):
        BasicView._draw_routine(self)

        glLoadIdentity() 
        glColor4f(0, 0, 0, 0.25)
        
        # border
        draw_func(self._working_rect, self._points,
                  self._modifiers, GL_LINE_STRIP)


class BeveledInlay(BeveledBorder):


    def _draw_routine(self):
        BeveledBorder._draw_routine(self)
        r = self._working_rect
        
        corner_left = r.left + self._corner_size
        corner_right = r.right - self._corner_size
        corner_bottom = r.bottom + self._corner_size
        corner_top = r.top - self._corner_size
        
        r.width -= 2
        r.height -= 2
        r.right -= 1
        r.top -= 1

        # shadows
        glColor4f(0, 0, 0, 0.25)
        glBegin(GL_LINE_STRIP)

        glVertex2f(corner_left -1, r.bottom)
        glVertex2f(r.left, corner_bottom -1)
        glVertex2f(r.left, corner_top)
        glVertex2f(corner_left, r.top)
        glVertex2f(corner_right -1, r.top)

        glEnd()

        # lights
        glColor4f(1, 1, 1, 0.5)
        glBegin(GL_LINE_STRIP)

        glVertex2f(corner_right - 1, r.top)
        glVertex2f(r.right, corner_top - 1)
        glVertex2f(r.right, corner_bottom)
        glVertex2f(corner_right, r.bottom)
        glVertex2f(corner_left - 1, r.bottom)

        glEnd()

        r.width -= 2
        r.height -= 2
        r.right -= 1
        r.top -= 1

        # shadows
        glColor4f(0, 0, 0, 0.125)
        glBegin(GL_LINE_STRIP)

        glVertex2f(corner_left -1, r.bottom)
        glVertex2f(r.left, corner_bottom -1)
        glVertex2f(r.left, corner_top)
        glVertex2f(corner_left, r.top)
        glVertex2f(corner_right -1, r.top)

        glEnd()


        # lights
        glColor4f(1, 1, 1, 0.25)
        glBegin(GL_LINE_STRIP)

        glVertex2f(corner_right - 1, r.top)
        glVertex2f(r.right, corner_top - 1)
        glVertex2f(r.right, corner_bottom)
        glVertex2f(corner_right, r.bottom)
        glVertex2f(corner_left - 1, r.bottom)

        glEnd()


class BeveledOutlay(BeveledBorder):

    def _draw_routine(self):

        BeveledBorder._draw_routine(self)
        r = self._working_rect
        
        corner_left = r.left + self._corner_size
        corner_right = r.right - self._corner_size
        corner_bottom = r.bottom + self._corner_size
        corner_top = r.top - self._corner_size
        
        r.width -= 2
        r.height -= 2
        r.right -= 1
        r.top -= 1

        # lights
        glColor4f(1, 1, 1, 0.5)
        glBegin(GL_LINE_STRIP)

        glVertex2f(corner_left -1, r.bottom)
        glVertex2f(r.left, corner_bottom -1)
        glVertex2f(r.left, corner_top)
        glVertex2f(corner_left, r.top)
        glVertex2f(corner_right -1, r.top)

        glEnd()

        # shadows
        glColor4f(0, 0, 0, 0.25)
        glBegin(GL_LINE_STRIP)

        glVertex2f(corner_right - 1, r.top)
        glVertex2f(r.right, corner_top - 1)
        glVertex2f(r.right, corner_bottom)
        glVertex2f(corner_right, r.bottom)
        glVertex2f(corner_left - 1, r.bottom)

        glEnd()

        r.width -= 2
        r.height -= 2
        r.right -= 1
        r.top -= 1

        # lights
        glColor4f(1, 1, 1, 0.25)
        glBegin(GL_LINE_STRIP)

        glVertex2f(corner_left -1, r.bottom)
        glVertex2f(r.left, corner_bottom -1)
        glVertex2f(r.left, corner_top)
        glVertex2f(corner_left, r.top)
        glVertex2f(corner_right -1, r.top)

        glEnd()


        # shadows
        glColor4f(0, 0, 0, 0.125)
        glBegin(GL_LINE_STRIP)

        glVertex2f(corner_right - 1, r.top)
        glVertex2f(r.right, corner_top - 1)
        glVertex2f(r.right, corner_bottom)
        glVertex2f(corner_right, r.bottom)
        glVertex2f(corner_left - 1, r.bottom)

        glEnd()

############################################################################

# background views #########################################################
# mach ich spaeter noch

class Background(BasicView):
    pass

class BeveledBackground(BasicView):
    pass

class MenuBg(BasicBeveledView):

    def _draw_routine(self):
        BasicBeveledView._draw_routine(self)
        glColor4f(224/255.0, 223/255.0, 222/255.0, 255)
        draw_func(self._working_rect, self._points[:-1],
                  self._modifiers[:-1], GL_POLYGON)



class MenuItemBg(BasicBeveledView):

    def _draw_routine(self):
        BasicBeveledView._draw_routine(self)
        glColor4f(0, 0, 0, 0.125)
        draw_func(self._working_rect, self._points[:-1],
                  self._modifiers[:-1], GL_POLYGON)

############################################################################
